#include<iostream>
#include<deque>
#include<cstring>
using namespace std;
typedef pair<int,int> PII;
const int N=510;
char numset[N][N];
int weightset[N][N];

int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};

int main()
{
int n,m;
	while(cin>>n>>m,n&&m)
	{
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				cin>>numset[i][j];
			}
		}

		int x1,y1,x2,y2;cin>>x1>>y1>>x2>>y2;

		memset(weightset,0x3f,sizeof weightset);
		//01
		{
			deque<PII> dq;

			dq.push_back({x1,y1});
			weightset[x1][y1]=0;

			while(!dq.empty())
			{
				int x=dq.front().first;
				int y=dq.front().second;
				dq.pop_front();
				if(x==x2&&y==y2)
				{
					cout<<weightset[x2][y2]<<endl;
					break;
				}
				for(int i=0;i<4;i++)
				{
					int a=x+dx[i];
					int b=y+dy[i];
					if(a>=0&&b>=0&&a<n&&b<m)
					{
						int w=(numset[a][b]==numset[x][y]?0:1);
						if(weightset[x][y]+w<weightset[a][b])
						{
							if(w==1)
							{
								dq.push_back({a,b});
							}
							else
							{
								dq.push_front({a,b});
							}
							weightset[a][b]=weightset[x][y]+w;
						}
					}
				}
			}
		}
	}
}